package com.yxjx.duoxue.share;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.comment.AddingCommentsActivity;
import com.yxjx.duoxue.customview.YxGridView;
import com.yxjx.duoxue.images.TestPicActivity;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class SharePopupWindow extends PopupWindow {
	
	private static final String []titles = new String[] {
			"微信好友",
			"朋友圈",
			"QQ空间",
			"微博",
			"QQ"
	};
	private static final int[] icons = new int[] {
			R.drawable.t_wxhy,
			R.drawable.t_pyq,
			R.drawable.t_qzone,
			R.drawable.t_weibo,
			R.drawable.t_qq
	};
	
	private Callback mCallback;
	
	public interface Callback {
		void onItemClick(int drawableId);
	}

	public SharePopupWindow(Context mContext, View parent) {

		View view = View
				.inflate(mContext, R.layout.segment_share_item_grid, null);
		// customize grid view.
		YxGridView grid = (YxGridView) view.findViewById(R.id.share_item_grid);
		grid.setAdapter(new GridAdapter());
		grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Utils.logd("share click item = " + arg2);
				if (mCallback != null) {
					mCallback.onItemClick(icons[arg2]);
				}
				dismiss();
			}
		});
		//
		
		view.startAnimation(AnimationUtils.loadAnimation(mContext,
				R.anim.fade_ins));
		LinearLayout ll_popup = (LinearLayout) view
				.findViewById(R.id.ll_popup);
		ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
				R.anim.push_bottom_in_2));

		setWidth(LayoutParams.FILL_PARENT);
		setHeight(LayoutParams.FILL_PARENT);
		setBackgroundDrawable(new BitmapDrawable());
		setFocusable(true);
		setOutsideTouchable(true);
		setContentView(view);
		showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		update();

		TextView bt3 = (TextView) view
				.findViewById(R.id.item_popupwindows_cancel);
		bt3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dismiss();
			}
		});
	}
	
	public Callback getCallback() {
		return mCallback;
	}

	public void setCallback(Callback mCallback) {
		this.mCallback = mCallback;
	}

	class GridAdapter extends BaseAdapter {
		

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret;
			if (arg1 == null) {
				ret = LayoutInflater.from(arg2.getContext()).inflate(R.layout.item_share_window, null);
			} else {
				ret = arg1;
			}
			
			UIHelper.setText(ret, R.id.title, titles[arg0]);
			((ImageView) ret.findViewById(R.id.icon)).setBackgroundResource(icons[arg0]);
			return ret;
		}
		
	}
}
